package com.gimi.cloud.bbp.merchantZone.consumer.feign.config;

import feign.RequestTemplate;
import feign.Util;
import feign.form.spring.SpringMultipartEncodedDataProcessor;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;

public class MySpringMultipartEncodedDataProcessor extends SpringMultipartEncodedDataProcessor{
    private static final String CRLF = "\r\n";

    protected boolean isPayload(Object value) {
        if(value.getClass().equals(LinkedList.class)){
            LinkedList<Object> datas = (LinkedList<Object>) value;
            return null!=datas && datas.size()>0 && super.isPayload(datas.get(0));
        }else {
            return super.isPayload(value);
        }
    }

    public void process(Map<String, Object> data, RequestTemplate template) {
        try {
            String boundary = this.createBoundary();
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

            try {
                PrintWriter writer = new PrintWriter(outputStream);

                for(Iterator var6 = data.entrySet().iterator(); var6.hasNext(); writer.append(CRLF).flush()) {
                    Map.Entry<String, Object> entry = (Map.Entry)var6.next();
                    writer.append("--" + boundary).append(CRLF);
                    if (this.isPayload(entry.getValue())) {
                        this.myWriteByteOrFile(outputStream, writer, (String)entry.getKey(), entry.getValue(),boundary);
                    } else {
                        this.writeParameter(writer, (String)entry.getKey(), entry.getValue().toString());
                    }
                }

                writer.append("--" + boundary + "--").append(CRLF).flush();
            } catch (Throwable var9) {
                try {
                    outputStream.close();
                } catch (IOException var8) {
                    ;
                }

                throw var9;
            }

            String contentType = CONTENT_TYPE + "; boundary=" + boundary;
            template.header("Content-Type", new String[]{contentType});
            template.body(outputStream.toByteArray(), Util.UTF_8);
            outputStream.close();
        } catch (Throwable var10) {
            try {
                throw var10;
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    private String createBoundary() {
        return Long.toHexString(System.currentTimeMillis());
    }

    private void writeParameter(PrintWriter writer, String name, String value) {
        writer.append("Content-Disposition: form-data; name=\"" + name + "\"").append(CRLF);
        writer.append("Content-Type: text/plain; charset=UTF-8").append(CRLF);
        writer.append(CRLF).append(value);
    }


    protected void myWriteByteOrFile(OutputStream output, PrintWriter writer, String name, Object value,String boundary) {
        if(value.getClass().equals(LinkedList.class)){
            LinkedList<Object> datas = (LinkedList<Object>) value;
            int i = 0;
            for(Object val : datas){
                if(i!=0){
                    writer.append(CRLF).flush();
                    writer.append("--" + boundary).append(CRLF);
                }
                super.writeByteOrFile(output, writer, name, val);
                i++;
            }
        }else {
            super.writeByteOrFile(output, writer, name, value);
        }
    }
}
